<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/6/13 0013
 * Time: 10:12
 */
namespace App\Ordersys\Repositories;

use App\Ordersys\Contracts\OrdersysOrderInterface;
use App\Ordersys\Exceptions\OrdersysException;
use App\Ordersys\Models\OrdersysCartModel;
use App\Ordersys\Models\OrdersysOrderExpressModel;
use App\Ordersys\Models\OrdersysOrderGoodsModel;
use App\Ordersys\Models\OrdersysOrderModel;
use Illuminate\Support\Facades\Request;


/**
 * 下订单列表
 * Class DbOrdersysOrderRepository
 * @package App\Ordersys\Repositories
 */
class DbOrdersysOrderRepository implements OrdersysOrderInterface
{

    /**
     * @return OrdersysOrderModel
     */
    public function getOrdersysOrderModel(){
        return app()->make(OrdersysOrderModel::class);
    }

    /**
     * 订单购物车
     * @return OrdersysCartModel
     */
    public function getOrdersysCartModel(){
        return app()->make(OrdersysCartModel::class);
    }


    /**
     * 订单产品模型
     * @return OrdersysOrderGoodsModel
     */
    public function getOrdersysOrderGoodsModel(){
        return app()->make(OrdersysOrderGoodsModel::class);
    }

    /**
     *
     * @return OrdersysOrderExpressModel
     */
    public function getOrdersysOrderExpressModel(){
        return app()->make(OrdersysOrderExpressModel::class);
    }
    /**
     * 获取订单产品列表
     * @param $where
     * @return mixed
     */
    public function getList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'add_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysOrderModel()->newQuery()->select($select)->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    public function getOrderListExpressList($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'add_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysOrderModel()->newQuery()->with("express")->select([$this->getOrdersysOrderExpressModel()->table .".*",$this->getOrdersysOrderModel()->table.".*"])->with("express")
        ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderModel()->table . '.id')
            ->where($where)->orderBy($sort,$order)->paginate($limit);
    }

    public function getOrderListExpressListAll($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'add_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysOrderModel()->newQuery()->with("express")->select([$this->getOrdersysOrderExpressModel()->table .".*",$this->getOrdersysOrderModel()->table.".*"])->with("goods","express")
            ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderModel()->table . '.id')
            ->where($where)->orderBy($sort,$order)->get($limit);
    }

    public function getOrderListExpressInfo($where){
        $request=Request::instance();
        $limit=empty($request->input("limit"))?10:$request->input("limit");
        $sort=empty($request->input("sort"))?'add_time':$request->input("sort");
        $order=empty($request->input("order"))?'desc':$request->input("order");
        $select=empty($request->input("select")) ? '*' : explode(",",$request->input("select"));
        return $this->getOrdersysOrderModel()->newQuery()->with("express")->select([$this->getOrdersysOrderExpressModel()->table .".*",$this->getOrdersysOrderModel()->table.".*"])->with("express")
            ->leftJoin($this->getOrdersysOrderExpressModel()->table, $this->getOrdersysOrderExpressModel()->table . '.order_id', '=', $this->getOrdersysOrderModel()->table . '.id')
            ->where($where)->first();
    }

    /**
     * 保存公告数据
     * @param $data
     * @return mixed
     */
    public function saveData($data){
        if(empty($data['status'])){
            $data['status']=0;
        }
        $data["uid"]=UID;
      return $this->getOrdersysOrderModel()->newQuery()->updateOrCreate(["id"=>$data["id"]],$data);
    }

    /**
     * 保存快递信息
     * @param $data
     * @return bool
     */
    public function saveOrderExpresData($data){
        $data=$this->getOrdersysOrderExpressModel()->filter($data);
        $data["create_time"]=time();
        $data["update_time"]=time();
        return $this->getOrdersysOrderExpressModel()->newQuery()->insert($data);
    }
    /**
     * 保存快递信息
     * @param $data
     * @return bool
     */
    public function updateOrderExpresData($data){
        unset($data['id']);
        $data=$this->getOrdersysOrderExpressModel()->filter($data);
       // $data["create_time"]=time();
        $data["update_time"]=time();
        return $this->getOrdersysOrderExpressModel()->newQuery()->where("order_id",$data["order_id"])->update($data);
    }

    /**
     * @param $orderId
     * @return bool|mixed
     * @throws OrdersysException
     */
    public function saveOrderGoodsData($orderId){
        $models=$this->getOrdersysCartModel();
        $where["buyer_id"]=UID;
        $list=$models->listCart("db",$where);
        if(empty($list)){
            throw  new OrdersysException(OrdersysException::EROOR_CART_EXIT_NOT);
        }
        $insertArr=array();
        foreach ($list as $ck=>$cv){
            unset($cv["id"]);
            $insertArr[$ck]=array_merge($cv,["order_id"=>$orderId]);
        }
//        print_r($insertArr);
//        exit;
         if($this->getOrdersysOrderGoodsModel()->newQuery()->insert($insertArr)){
               //清空购物车
            return  $this->getOrdersysCartModel()->delCart("db",["buyer_id"=>UID]);
         }else{
             throw new OrdersysException(OrdersysException::EROOR_CART_EXIT_NOT);
         }

    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed
     */
    public function getInfo($id){
        return $this->getOrdersysOrderModel()->newQuery()->where("id",$id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where){
        return $this->getOrdersysOrderModel()->newQuery()->where($where)->delete();
    }

    /**
     * 添加到购物车
     * @param $data
     * @return mixed
     */
    public function addCart($data){
        $data['buyer_id']=UID;  //添加购物车的用户id
     return $this->getOrdersysCartModel()->addCart($data,"db",$data['goods_num']);
    }

    public function clearCart($where){

        return $this->getOrdersysCartModel()->delCart("db",$where);
    }

    public function getCartIds($where){
        return $this->getOrdersysCartModel()->newQuery()->where($where)->pluck("goods_id");
    }
    public function getCartAllPrice(){

        $models=$this->getOrdersysCartModel();
        $models->getCartNum("db",array('buyer_id'=>UID));
        return   $models->cart_all_price;
    }

    /**
     * 获取购物车列表
     * @param $where
     * @return OrdersysCartModel[]|array|\Illuminate\Database\Eloquent\Collection|mixed
     */
    public function getCartList($where){
        $models=$this->getOrdersysCartModel();
       return  $models->listCart("db",$where);
    }


    /**
     * 订单产品
     * @param $where
     * @return mixed|void
     */
    public function getOrderGoodsList($where){
      return $this->getOrdersysOrderGoodsModel()->newQuery()->where($where)->paginate(50);
    }

    public function deleteOrderData($id){
        $this->getOrdersysOrderModel()->newQuery()->where("id",$id)->delete();
        $this->getOrdersysOrderExpressModel()->newQuery()->where("order_id",$id)->delete();
        return $this->getOrdersysOrderGoodsModel()->newQuery()->where("order_id",$id)->delete();
    }
}
